/* main function to use all the ones that contained in .libs from other three projs*/
#pragma once
#include <cxcore.h>
#include <cv.h>
#include <highgui.h>
#include <math.h>
#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include <direct.h>
#include "display.h"
#include "comparison_frame_difference.h"
using namespace std;

char dirname[300], filename[300], filename1[300], filename2[300], filenamev[300], filenames[300];
IplImage *src1_c, *src2_c, *frame = 0, *frame_copy = 0, *frame_crop = 0, *frame_flip = 0, *frame_anchor = 0, *frame_target = 0;
//typedef struct CvCapture CvCapture;
CvCapture *vcap = 0;
//typedef struct CvCapture vcap;

void main(void){

	cout<<"please input the filename"<<endl;

	sprintf(filenamev,"C:/Program Files/Dr Robot i90 Robot Explorer/CLIP_20081104-225216.avi");
//	sprintf(filenamev,"C:/Program Files/Dr Robot i90 Robot Explorer/CLIP_20081104-094233.avi");
//	sprintf(filenamev,"C:/Program Files/Dr Robot i90 Robot Explorer/CLIP_20081104-222809.avi");
//	sprintf(filenamev,"C:/Program Files/Dr Robot i90 Robot Explorer/CLIP_20081104-223554.avi");
//	sprintf(filenamev,"C:/Program Files/Dr Robot i90 Robot Explorer/CLIP_20081104-224210.avi");
	
	sprintf(filename1,"E:/DOC/My Pictures/QuickCam/picture 17.jpg");	//23
	sprintf(filename2,"E:/DOC/My Pictures/QuickCam/picture 18.jpg");	//24
//	sprintf(filenamev,"E:/DOC/My Pictures/QuickCam/Video/valsalva_without_cross Converted.avi");	//24
//	sprintf(filenamev,"E:/DOC/My Pictures/QuickCam/Video/MVI_5333.avi");	//24
//	sprintf(filenamev,"E:/DOC/My Pictures/QuickCam/Video/D.avi");	//24
//	sprintf(filenamev,"E:/DOC/My Pictures/QuickCam/Video/Robot_sim_320_240.avi");	//24

	vcap = cvCaptureFromFile(filenamev);

	sprintf(dirname,"C:/Program Files/Dr Robot i90 Robot Explorer/CLIP_20081104-225216/");
//	sprintf(dirname,"C:/Program Files/Dr Robot i90 Robot Explorer/CLIP_20081104-094233/");
//	sprintf(dirname,"C:/Program Files/Dr Robot i90 Robot Explorer/CLIP_20081104-222809/");
//	sprintf(dirname,"C:/Program Files/Dr Robot i90 Robot Explorer/CLIP_20081104-223554/");
//	sprintf(dirname,"C:/Program Files/Dr Robot i90 Robot Explorer/CLIP_20081104-224210/");
	
	mkdir(dirname);
		
	int TimeStampHeight = 16;

	if(vcap != NULL){
		for (int c = 0; ((frame = cvQueryFrame(vcap)) != NULL); c++ ){
			if( c == 0) {
				sprintf(filenames,"%sframe%d.jpg",dirname,c);	
				frame_target = cvCloneImage(frame);	//ONLY RUN ONCE IN ITERATION 0
				cvSetImageROI(frame_target,cvRect(0,0,frame_target->width,frame_target->height - TimeStampHeight));
				frame_crop = cvCreateImage(cvSize(frame_target->width,frame_target->height - TimeStampHeight),8,3);
				frame_flip = cvCreateImage(cvSize(frame_target->width,frame_target->height - TimeStampHeight),8,3);
				cvCopyImage(frame_target,frame_crop);
				cvFlip(frame_target,frame_flip,0);
				cvSaveImage(filenames,frame_flip);
				continue;
			}
			frame_anchor = cvCloneImage(frame);		//
			sprintf(filenames,"%sframe%d.jpg",dirname,c);	
			frame_anchor = cvCloneImage(frame);
			cvSetImageROI(frame_anchor,cvRect(0,0,frame_anchor->width,frame_anchor->height - TimeStampHeight));
			frame_crop = cvCreateImage(cvSize(frame_anchor->width,frame_anchor->height - TimeStampHeight),8,3);
			frame_flip = cvCreateImage(cvSize(frame_anchor->width,frame_anchor->height - TimeStampHeight),8,3);
			cvCopyImage(frame_anchor,frame_crop);
			cvFlip(frame_crop,frame_flip,0);

			//IF frame_anchor IS DIFFERENT FROM frame_target
			if(compare_frame_difference(frame_target,frame_anchor))
			{
				cvSaveImage(filenames,frame_flip);

				if((c%100) == 0)	cout<<"Frame "<<c<<endl;

				frame_target = cvCloneImage(frame_anchor);
				cvWaitKey(0);
				cvReleaseImage(&frame_copy);
				cvReleaseImage(&frame_crop);
			}
		}
	}
	else cout << "Video file is not compatible."<<endl;
	cvWaitKey(0);
	cvReleaseCapture(&vcap);
}